<?php
namespace app\admin\controller;
use app\admin\validate\UserValidate;
use app\admin\model\BusinessModel;
use think\facade\Request;

/**
 * 用户管理控制器
 * Class Business
 * @package app\admin\controller
 */
class Business extends UserValidate{
    /**用户管理控制器  首页
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function index(){
        $add = new BusinessModel;
        $Businessindex = $add->Index(input('ma_state'));
        $this->assign('Businessindex',$Businessindex);
        return view();
    }

    /**
     * 搜索
     */
    public function search(){

    }

    /**添加商户
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function BusinessAdd(){
        $value_post = input('value');
        if (empty($value_post)){
            return view();
        }
        if (empty($_FILES['ma_logo']['name'])){
            $this->error('请上传图片');
        }
        $file = request()->file('ma_logo');
        if ($file){
            $target = $file->rule('md5')->validate(['ext'=>'jpg,png,gif'])->move( '../public/image',time());
            if ($target == false){
                $this->error('请检查图片');
            }
        }
        $translation = "/image/" .$target->getsaveName();
        $value_add = (new BusinessModel)->storeAdd(Request::post(),$translation);
        if ($value_add == true){
            $this->success('新增成功');
        } else{
            $this->error($value_add);
        }
    }

    /**
     * 商户渲染/删除
     * ma_id   商户  id
     * move     是否删除Gaoyang123
     * @return \think\response\View
     */
    public function BusinessChange(){
        if (empty(input('ma_id'))){
            $this->error('缺少参数');
        }
        if (input('move')){
            $out = (new BusinessModel)->modifyChange(input('ma_id'),input('move'));
            if ($out == false){
                $this->error('删除失败');
            }
            $this->success('删除成功');
        }
        $modifyChange = (new BusinessModel)->modifyChange(input('ma_id'));
        if ($modifyChange == false){
            $this->error('商户不存在');
        }
        $division = explode('-',$modifyChange['ma_business_hours']);
        $modifyChange['ma_business_hours'] = $division['0'];
        $modifyChange['ma_business_hoursl'] = $division['1'];
        $this->assign('modifyChange',$modifyChange);
        return view();
    }

    /**
     * 商户修改信息
     */
    public function BusinessAmend(){
        if (!empty($_FILES['ma_logo']['name'])){
            $file = request()->file('ma_logo');

            if ($file){
                $target = $file->rule('md5')->validate(['size'=>300000,'ext'=>'jpg,png,gif'])->move( '../public/image',time());

                if ($target == false){
                    $this->error('请检查图片');
                }
            }
            $translation = "/image/" .$target->getsaveName();
            $value_add = (new BusinessModel)->storeAmend(Request::post(),$translation);
            if ($value_add == true){
                $this->success('修改成功');
            }
            $this->success("$value_add");
        }
        $value_add = (new BusinessModel)->storeAmend(Request::post(),$translation=null);
        if ($value_add === true){
            $this->success('修改成功');
        }
        $this->success("$value_add");
    }


    /**个人信息
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function BusinessUser(){
        $user = (new BusinessModel)->userIndex();
        $this->assign('user',$user);
        return view();
    }
    public function del(){
       (new BusinessModel)->deleteUser((int)input('param.id'));
        $this->success('删除成功');
    }
}